package db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Mads Nielsen <k4emic@gmail.com>
 */
public class DBConnection {
    
    private static DBConnection instance;
    
    private Connection connection;
    
    private DBConnection() {
    }
    
    public static DBConnection getInstance() {
        
        if(instance == null) {
            instance = new DBConnection();
            try {
                instance.openConnection();
            } catch (SQLException ex) {
                Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
        return instance;
    }
    
    public void openConnection() throws SQLException {
        String username = "DM75_5";
        String password = "MaaGodt";
        String hostname = "balder.ucn.dk";
        String database = "dm75_5";
        
        String connectionUrl = String.format("jdbc:sqlserver://%s;databaseName=%s;user=%s;password=%s",
                hostname,  database, username, password);
        
        connection = DriverManager.getConnection(connectionUrl);
    }
    
    public void closeConnection() {
        try {
            connection.close();
        } catch (SQLException ex) {
            Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public Connection getConnection() {
        return connection;
    }
}
